package com.ruoyi.contest.service.impl;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.contest.domain.TAnalyze;
import com.ruoyi.contest.domain.vo.AwardGradeClassifyVO;
import com.ruoyi.contest.mapper.TAnalyzeMapper;
import com.ruoyi.contest.service.ITAnalyzeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 数据分析Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@Service
public class TAnalyzeServiceImpl extends ServiceImpl<TAnalyzeMapper, TAnalyze> implements ITAnalyzeService {
    /**
     * 查询数据分析列表
     *
     * @param tAnalyze 数据分析
     * @return 数据分析
     */
    @Override
    public List<TAnalyze> selectTAnalyzeList(TAnalyze tAnalyze) {
        return baseMapper.selectTAnalyzeList(tAnalyze);
    }

    @Autowired
    private TAnalyzeMapper tAnalyzeMapper;

    /**
     * 查询grade分类和统计
     *
     * @param param
     * @return
     */
    public List selectCountAndClassify(String param) {

        ObjectMapper objectMapper = new ObjectMapper();
        //定义分类统计变量
        AtomicReference<Long> count_grade_A = new AtomicReference<>(0L);
        AtomicReference<Long> count_grade_B = new AtomicReference<>(0L);
        AtomicReference<Long> count_grade_C = new AtomicReference<>(0L);
        AtomicReference<Long> count_grade_D = new AtomicReference<>(0L);
        AtomicReference<Long> count_grade_E = new AtomicReference<>(0L);

        //定义award_grade分类统计变量
        AtomicReference<Long> count_award_grade_0 = new AtomicReference<>(0L);
        AtomicReference<Long> count_award_grade_1 = new AtomicReference<>(0L);
        AtomicReference<Long> count_award_grade_2 = new AtomicReference<>(0L);
        AtomicReference<Long> count_award_grade_3 = new AtomicReference<>(0L);
        AtomicReference<Long> count_award_grade_4 = new AtomicReference<>(0L);

        Map<String, Object> gradeClassifyMap = new HashMap<>();
        Map<String, Object> awardMap = new HashMap<>();

        //TODO 1 先查询到数据 这里的数据是默认的按A-E分类之后的0-4子分类
        List list = tAnalyzeMapper.selectCountAndClassify(param);

//        System.err.println("default:" + list);

        List forkList = list;//备份list
        //计算A-E分类总数
        list.forEach(item -> {
            Map<String, Object> stringObjectMap = objectMapper.convertValue(item, Map.class);
            //
            if ("A".equals(stringObjectMap.get("grade"))) {
                count_grade_A.updateAndGet(v -> v + (Long) stringObjectMap.get("count"));
            } else if ("B".equals(stringObjectMap.get("grade"))) {
                count_grade_B.updateAndGet(v -> v + (Long) stringObjectMap.get("count"));
            } else if ("C".equals(stringObjectMap.get("grade"))) {
                count_grade_C.updateAndGet(v -> v + (Long) stringObjectMap.get("count"));
            } else if ("D".equals(stringObjectMap.get("grade"))) {
                count_grade_D.updateAndGet(v -> v + (Long) stringObjectMap.get("count"));
            } else if ("E".equals(stringObjectMap.get("grade"))) {
                count_grade_E.updateAndGet(v -> v + (Long) stringObjectMap.get("count"));
            } else {//TODO 处理未定义的情况

            }
        });
        gradeClassifyMap.put("A", count_grade_A.get());//A的0-4的总数
        gradeClassifyMap.put("B", count_grade_B.get());
        gradeClassifyMap.put("C", count_grade_C.get());
        gradeClassifyMap.put("D", count_grade_D.get());
        gradeClassifyMap.put("E", count_grade_E.get());//grade分类统计数据
//        System.err.println("############" + JSON.toJSONString(gradeClassifyMap));

        //定义变量
        AtomicReference<Long> d_classify_0 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_1 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_2 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_3 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_4 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_5 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_6 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_7 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_8 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_9 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_10 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_11 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_12 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_13 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_14 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_15 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_16 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_17 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_18 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_19 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_20 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_21 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_22 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_23 = new AtomicReference<>(0L);
        AtomicReference<Long> d_classify_24 = new AtomicReference<>(0L);

        //从A-E类中提取出0-4的各自占比
        list.forEach(m -> {
            Map<String, Object> map = objectMapper.convertValue(m, Map.class);
            if ("0".equals(map.get("a_grade")) && "A".equals(map.get("grade"))) {
                d_classify_0.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("1".equals(map.get("a_grade")) && "A".equals(map.get("grade"))) {
                d_classify_1.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("2".equals(map.get("a_grade")) && "A".equals(map.get("grade"))) {
                d_classify_2.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("3".equals(map.get("a_grade")) && "A".equals(map.get("grade"))) {
                d_classify_3.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("4".equals(map.get("a_grade")) && "A".equals(map.get("grade"))) {
                d_classify_4.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("0".equals(map.get("a_grade")) && "B".equals(map.get("grade"))) {
                d_classify_5.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("1".equals(map.get("a_grade")) && "B".equals(map.get("grade"))) {
                d_classify_6.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("2".equals(map.get("a_grade")) && "B".equals(map.get("grade"))) {
                d_classify_7.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("3".equals(map.get("a_grade")) && "B".equals(map.get("grade"))) {
                d_classify_8.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("4".equals(map.get("a_grade")) && "B".equals(map.get("grade"))) {
                d_classify_9.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("0".equals(map.get("a_grade")) && "C".equals(map.get("grade"))) {
                d_classify_10.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("1".equals(map.get("a_grade")) && "C".equals(map.get("grade"))) {
                d_classify_11.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("2".equals(map.get("a_grade")) && "C".equals(map.get("grade"))) {
                d_classify_12.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("3".equals(map.get("a_grade")) && "C".equals(map.get("grade"))) {
                d_classify_13.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("4".equals(map.get("a_grade")) && "C".equals(map.get("grade"))) {
                d_classify_14.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("0".equals(map.get("a_grade")) && "D".equals(map.get("grade"))) {
                d_classify_15.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("1".equals(map.get("a_grade")) && "D".equals(map.get("grade"))) {
                d_classify_16.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("2".equals(map.get("a_grade")) && "D".equals(map.get("grade"))) {
                d_classify_17.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("3".equals(map.get("a_grade")) && "D".equals(map.get("grade"))) {
                d_classify_18.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("4".equals(map.get("a_grade")) && "D".equals(map.get("grade"))) {
                d_classify_19.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("0".equals(map.get("a_grade")) && "E".equals(map.get("grade"))) {
                d_classify_20.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("1".equals(map.get("a_grade")) && "E".equals(map.get("grade"))) {
                d_classify_21.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("2".equals(map.get("a_grade")) && "E".equals(map.get("grade"))) {
                d_classify_22.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("3".equals(map.get("a_grade")) && "E".equals(map.get("grade"))) {
                d_classify_23.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("4".equals(map.get("a_grade")) && "E".equals(map.get("grade"))) {
                d_classify_24.updateAndGet(v -> v + (Long) map.get("count"));
            }
        });


        //TODO X类中0-4的占比
        Map<String,Object> A_vo = new HashMap<>();
        Map<String,Object> B_vo = new HashMap<>();
        Map<String,Object> C_vo = new HashMap<>();
        Map<String,Object> D_vo = new HashMap<>();
        Map<String,Object> E_vo = new HashMap<>();

        List gradeResultList = new ArrayList<>();

        //TODO 组装A类
        Map<String, Object> AMap = new HashMap<>();
        AMap.put("0", d_classify_0.get());//A类的特等
        AMap.put("1", d_classify_1.get());//A类的一等
        AMap.put("2", d_classify_2.get());//A类的二等
        AMap.put("3", d_classify_3.get());//A类的三等
        AMap.put("4", d_classify_4.get());//A类的是四

        A_vo.put("grade","A");
        A_vo.put("award_grade_map",AMap);

        //TODO 组装B类
        Map<String, Object> BMap = new HashMap<>();
        BMap.put("0", d_classify_5.get());//B类的特等
        BMap.put("1", d_classify_6.get());//B类的一等
        BMap.put("2", d_classify_7.get());//B类的二等
        BMap.put("3", d_classify_8.get());//B类的三等
        BMap.put("4", d_classify_9.get());//B类的是四

        B_vo.put("grade","B");
        B_vo.put("award_grade_map",BMap);

        //TODO 组装C类
        Map<String, Object> CMap = new HashMap<>();
        CMap.put("0", d_classify_10.get());//C类的特等
        CMap.put("1", d_classify_11.get());//C类的一等
        CMap.put("2", d_classify_12.get());//C类的二等
        CMap.put("3", d_classify_13.get());//C类的三等
        CMap.put("4", d_classify_14.get());//C类的是四

        C_vo.put("grade","C");
        C_vo.put("award_grade_map",CMap);

        //TODO 组装D类
        Map<String, Object> DMap = new HashMap<>();
        DMap.put("0", d_classify_15.get());//D类的特等
        DMap.put("1", d_classify_16.get());//D类的一等
        DMap.put("2", d_classify_17.get());//D类的二等
        DMap.put("3", d_classify_18.get());//D类的三等
        DMap.put("4", d_classify_19.get());//D类的是四

        D_vo.put("grade","D");
        D_vo.put("award_grade_map",DMap);

        //TODO 组装E类
        Map<String, Object> EMap = new HashMap<>();
        EMap.put("0", d_classify_20.get());//E类的特等
        EMap.put("1", d_classify_21.get());//E类的一等
        EMap.put("2", d_classify_22.get());//E类的二等
        EMap.put("3", d_classify_23.get());//E类的三等
        EMap.put("4", d_classify_24.get());//E类的是四

        E_vo.put("grade","E");
        E_vo.put("award_grade_map",EMap);

        //TODO 组装成List
        gradeResultList.add(A_vo);
        gradeResultList.add(B_vo);
        gradeResultList.add(C_vo);
        gradeResultList.add(D_vo);
        gradeResultList.add(E_vo);

        //计算award_grade分类统计
        list.forEach(item -> {
            Map<String, Object> map = objectMapper.convertValue(item, Map.class);
            if ("0".equals(map.get("a_grade"))) {
                count_award_grade_0.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("1".equals(map.get("a_grade"))) {
                count_award_grade_1.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("2".equals(map.get("a_grade"))) {
                count_award_grade_2.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("3".equals(map.get("a_grade"))) {
                count_award_grade_3.updateAndGet(v -> v + (Long) map.get("count"));
            } else if ("4".equals(map.get("a_grade"))) {
                count_award_grade_4.updateAndGet(v -> v + (Long) map.get("count"));
            } else {//TODO 处理空情况

            }
        });

        awardMap.put("0", count_award_grade_0.get());
        awardMap.put("1", count_award_grade_1.get());
        awardMap.put("2", count_award_grade_2.get());
        awardMap.put("3", count_award_grade_3.get());
        awardMap.put("4", count_award_grade_4.get());

//        System.err.println("############" + JSON.toJSONString(awardMap));
        Map<String, Object> result = new HashMap<>();
        result.put("award_grade_classify", awardMap);//0-4
        result.put("grade_classify", gradeClassifyMap);//A-E
        result.put("default_classify", gradeResultList);//FIXME A-E分类0-4

        List res = new ArrayList<>();
        res.add(0, result);
        return res;
    }
}
